package com.example.demo.controller;

import com.example.demo.dto.OAformDto;
import com.example.demo.service.WeChatBoTService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;

@Controller
@CrossOrigin(origins = "*")
@ResponseBody
@Slf4j
public class ExternalRequestController {
    private final WeChatBoTService weChatBoTService;

    @Autowired
    public ExternalRequestController(WeChatBoTService weChatBoTService) {
        this.weChatBoTService = weChatBoTService;
    }

//    @PostMapping("/toOA")
//    public String toOA(@RequestBody OAformDto dto) throws Exception {
//        Instant timestamp = Instant.parse(dto.getGqrq());
//        LocalDateTime localDateTime = LocalDateTime.ofInstant(timestamp, ZoneId.systemDefault());
//        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
//        dto.setGqrq(localDateTime.format(formatter));
//
//        String body = GenerateReminderUtils.setReminder(dto);
//        ObjectMapper mapper = new ObjectMapper();
//        Map map = mapper.readValue(body, Map.class);
//        return map.get("code") != "SUCCESS" ? "已提醒工程师" : Objects.toString(map.get("errMsg"), "");
//    }

    @PostMapping("/toWeChat")
    public String toWeChat(@RequestBody OAformDto dto) {
        log.info("发送挂起提醒，挂起内容：{}", dto);
        String key = "fb567e21-4c69-498d-97f8-a06639688dca";
//        String key = "744181ac-b074-45b8-a541-f88492a9b959";
        Instant timestamp = Instant.parse(dto.getGqrq());
        LocalDateTime localDateTime = LocalDateTime.ofInstant(timestamp, ZoneId.systemDefault());
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        dto.setGqrq(localDateTime.format(formatter));
        weChatBoTService.sendMessage_Card(key, dto);
        return "已提醒微信";
    }
}
